home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 33 (1993-04)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 33 (1993-04)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / Programming / PostScript_IV / PostScript_IV
Text File  |  1993-04-14  |  19KB  |  640 lines

  1.                             Learning PostScript 
  2.  
  3.                                 ~ Part 4 ~ 
  4.  
  5.                               By Peter Goed 
  6.  
  7.  
  8.    || 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||
  9.  
  10.    Resizing and Repositioning
  11.  
  12.    Transformations
  13.    One of the more powerful aspects of PostScript is its ability to support
  14.   transformations of the coordinate system.  You can literally transform the
  15.   underlying page into something very different from that which we have been
  16.   working with for the previous three parts of this tutorial.
  17.  
  18.    This is an important concept to understand, when you use transformation
  19.   operators, you are changing the size, origin and/or orientation of the
  20.   underlying coordinate system.  This will in turn, transform the appearance
  21.   of anything that is subsequently dr awn on the page.
  22.  
  23.    In this month's tutorial we will examine the three most common kinds of
  24.   transformation: scaling, translation and rotation - each of which is
  25.   supported by a postscript operator.
  26.  
  27.    The 'scale' operator changes the unit size of either the x or y axis, or
  28.   both, without affecting their origin or orientation.
  29.  
  30.    The 'translate' operator moves the origin of the coordinate system to a
  31.   new location, while leaving unit size and orientation of the x/y axis
  32.   unchanged.
  33.  
  34.    The 'rotate' operator turns the axis of the coordinate system by any
  35.   specified angle, without affecting unit size or origin of the axis.
  36.  
  37.    Individually, these are powerful features.  Collectively, the ability to
  38.   scale, translate, and rotate any text and graphics means that with
  39.   PostScript, there is virtually nothing that can't be drawn on a page.
  40.  
  41.    scale
  42.    The ability to scale any object to any size is perhaps PostScript's single
  43.   most important capability.  This scaling gives the language its unique
  44.   ability to be used with any raster imaging device of any resolution, for
  45.   example from a 300 DPI laser to a 4800 DPI high quality image setter, or in
  46.   the future perhaps a 10,000 DPI imaging device.
  47.  
  48.    In tutorial 2 we briefly encountered scaling when we used the 'scalefont'
  49.   operator to scale typeface outlines to produce a font of any desired point
  50.   size.
  51.  
  52.    Unlike 'scalefont', which requires only one operand to scale typefaces
  53.   uniformly along both the x and y axis, the 'scale' operator requires two
  54.   operands for scaling any object independently or uniformly along either or
  55.   both axis.
  56.  
  57.    See how the instructions below create the same output as that in the code
  58.   in part 2 of these tutorials under 'show', but by using a 36 point font and
  59.   scaling it uniformly along the x and y axis by a factor of 2 to create a 72
  60.   point font.
  61.  
  62.         /Times-BoldItalic findfont scalefont 36 setfont
  63.  
  64.         gsave
  65.             72 648 moveto
  66.             2 2 scale
  67.             (Your Text) show
  68.         grestore
  69.  
  70.         showpage
  71.  
  72.  
  73.    Horizontal scaling
  74.    Shown in the following code are three ways to stretch the same 72 point
  75.   "Your Text" by scaling it only along the horizontal axis.  If you print
  76.   this out you will see that at y648, the 2 1 'scale' instruction, which
  77.   doubles the size for only the x axis, produces a string that appears too
  78.   long for the page.
  79.  
  80.    We can modify this by changing the scale factor for the axis to 1.75, as
  81.   at y504; or better still, by leaving the scale factor at 2 and using
  82.   'ashow' to draw the individual characters closer together as at y360.
  83.  
  84.         /Times-BoldItalic findfont 72 scalefont setfont
  85.  
  86.         gsave
  87.             72 648 moveto
  88.             2 1 scale
  89.             (Your Text) show
  90.         grestore
  91.  
  92.         gsave
  93.             72 504 moveto
  94.             1.75 1 scale
  95.             (Your Text) show
  96.         grestore
  97.  
  98.         gsave
  99.             72 360 moveto
  100.             2 1 scale
  101.             -5 0 (Your Text) ashow
  102.         grestore
  103.         showpage
  104.  
  105.  
  106.    Vertical scaling
  107.    Scaling text in the vertical direction produces an interesting change of
  108.   pace from the fonts we are accustomed to seeing.  If you print out the
  109.   following code, notice how scaling a 72 point font by a factor of 1.5
  110.   produces uppercase characters that are exactly one inch tall.  Remember
  111.   that point sizes allow for descenders as well as some white space above and
  112.   below the characters.
  113.  
  114.         /Times-Bold findfont 72 scalefont
  115.  
  116.         gsave
  117.             72 648 moveto
  118.             1 1 scale
  119.             -5 0 (Your Text 1.0) ashow
  120.         grestore
  121.  
  122.         gsave
  123.             72 504 moveto
  124.             1 1.5 scale
  125.             -5 0 (Your Text 1.5) ashow
  126.         grestore
  127.  
  128.         gsave
  129.             72 432 moveto
  130.             1 .5 scale
  131.             -5 0 (Your Text .5) ashow
  132.         grestore
  133.  
  134.         gsave
  135.             72 288 moveto
  136.             1 2 scale
  137.             -5 0 (Your Text 2.0) ashow
  138.         grestore
  139.  
  140.         gsave
  141.             72 72 moveto
  142.             1 3 scale
  143.             -5 0 (Your Text 3.0) ashow
  144.         grestore
  145.  
  146.         showpage
  147.  
  148.  
  149.    Emphasizing and Fitting Text
  150.    Vertical scaling is useful for emphasizing certain aspects of a graphic
  151.   (as in the word sale in the following page description), and also for
  152.   fitting into a width that is determined by the other elements of the
  153.   graphic (as in the last part of the description "THURSDAY, MAY 5").
  154.  
  155.         /Helvetica findfont 72 scalefont setfont
  156.  
  157.         72 648 moveto
  158.         (PRE-SEASON) show
  159.  
  160.         gsave
  161.             /Times-Bold findfont
  162.             144 scalefont setfont
  163.             72 191 moveto
  164.             1.25 4 scale
  165.             (SALE) show
  166.         grestore
  167.  
  168.         72 72 moveto
  169.         .72 1 scale
  170.         (THURSDAY, MAY 5) show
  171.  
  172.         showpage
  173.  
  174.    NOTE:- In the last line of the description we were able to use the
  175.   original 72 point Helvetica font without specifying it explicitly.  We used
  176.   'gsave' and 'grestore' to temporarily select the 144 point Times-Bold font
  177.   and then restored the 72 point Helvetica font as the current font.
  178.  
  179.  
  180.    A Word of Caution
  181.    When using the 'scale' operator, ensure that you use the 'gsave' and
  182.   'grestore' operators before and after it if you don't want succeeding
  183.   instructions to be affected.  This applies to object size as well as
  184.   location on the page.
  185.  
  186.    The instructions below show how text intended for coordinates 144, 288 is
  187.   moved to 72, 144 due to the previous 'scale' instruction.  Print it out and
  188.   you will get the message clearly.
  189.  
  190.         /Times-BoldItalic findfont 72 scalefont setfont
  191.         144 576 moveto
  192.         (Amiga One) show
  193.  
  194.         144 432 moveto
  195.         .5 .5 scale
  196.         (Amiga Two) show
  197.  
  198.         144 288 moveto
  199.         (Amiga Three) show    % Because of the .5 .5 scale used above
  200.                               % you will now have changed your page
  201.                               % coordinates to 50% of your intended size,
  202.                               % but not your type size, because it is
  203.                               % still .5 .5 scale of the 72 point font.
  204.         showpage
  205.  
  206.    As a general rule you should use 'gsave' and 'grestore' before and after
  207.   issuing 'scale' instructions, and do a 'moveto' to the desired location
  208.   before scaling.
  209.  
  210.  
  211.    Reflections
  212.    You can also use 'scale' to invert any object by specifying negative
  213.   values for either the x or y axis operands.  Negative x inverts the object
  214.   on the horizontal axis (right to left), and negative y inverts the object
  215.   bottom to top.  When both values are negative, the object is inverted in
  216.   both directions.
  217.  
  218.         /Helvetica-Bold findfont 36 scalefont setfont
  219.  
  220.         gsave
  221.             306 396 moveto
  222.             1 3 scale
  223.             (Amiga-1) show
  224.         grestore
  225.  
  226.         gsave
  227.            306 396 moveto
  228.             -1 3 scale
  229.            (Amiga-2) show
  230.         grestore
  231.  
  232.         gsave
  233.             306 396 moveto
  234.             1 -3 scale
  235.             (Amiga-3) show
  236.         grestore
  237.  
  238.         gsave
  239.             306 396 moveto
  240.             -1 -3 scale
  241.             (Amige-4) show
  242.         grestore
  243.  
  244.         showpage
  245.  
  246.  
  247.    Mirror
  248.    The same technique can be used effectively to create some interesting
  249.   effects using the 'setgray' operator, as shown in the following code.
  250.  
  251.         /Helvetica-Bold findfont 144 scalefont setfont
  252.  
  253.         gsave
  254.             54 396 moveto
  255.             1 3 scale
  256.             (PETER) show
  257.         grestore
  258.  
  259.         gsave
  260.             54 396 moveto
  261.             .8 setgray
  262.             1 -3 scale
  263.             (PETER) show
  264.         grestore
  265.  
  266.         showpage
  267.  
  268.  
  269.  
  270.    translate
  271.    Where 'scale' is used to resize elements of a page description, the
  272.   'translate' operator is used for repositioning certain elements of a page
  273.   description, or an entire page description.  It moves the origin of the
  274.   entire page from the lower-left corner to the x/y coordinates you specify
  275.   in the operands for 'translate'.
  276.  
  277.    To demonstrate the change, we'll compare two similarly produced examples.
  278.   For the first take our by now familiar instructions to produce "Your Text",
  279.   this time at coordinates 72, 72.
  280.  
  281.         /Times-BoldItalic findfont 72 scalefont setfont
  282.  
  283.         72 72 moveto
  284.         (Your Text) show
  285.  
  286.         showpage
  287.  
  288.  
  289.    Repositioning
  290.    You can achieve the same results as in the previous example with the
  291.   following instructions.
  292.  
  293.         /Times-BoldItalic findfont 72 scalefont setfont
  294.  
  295.         72 72 translate
  296.  
  297.         0 0 moveto
  298.         (Your Text) show
  299.  
  300.         showpage
  301.  
  302.    As you can see when you print out this and the previous examples, they are
  303.   both printed at the same location - with respect to the original coordinate
  304.   system, but because the 72, 72 'translate' instruction displaces the origin
  305.   of the underlying coordinate system, the moveto instruction is vastly
  306.   different.
  307.  
  308.    Thus 'translate' provides the ability to locate any set of instructions
  309.   anywhere on the page, making it extremely easy to relocate PostScript
  310.   routines.
  311.  
  312.  
  313.    Repetition
  314.    We can repeat the same result from the previous example anywhere on the
  315.   page by simply repeating the 'translate' instruction.  Because the 'scale'
  316.   and 'translate' instructions are not enclosed in 'gsave' and 'grestore',
  317.   the effects are cumulative, moving up the page 72 points each time.
  318.  
  319.         /Times-BoldItalic findfont 72 scalefont setfont
  320.  
  321.         72 72 translate
  322.         0 0 moveto
  323.         (Your Text) show
  324.  
  325.         0 72 translate
  326.         0 0 moveto
  327.         (Your Text) show
  328.  
  329.         0 72 translate
  330.         0 0 moveto
  331.         (Your Text) show
  332.  
  333.         % Etc.
  334.  
  335.         showpage
  336.  
  337.  
  338.    Similarity
  339.    With 'translate', we can re-use the same drawing routines, positioning the
  340.   results at different coordinates.
  341.  
  342.    Compare the following instructions with those in tutorial 3 headed
  343.   Fancy Text within a Shadow Box.  Because of
  344.   the 10 -10 'translate' in the shadow box routine below, we can initiate all
  345.   three boxes with the same "36 576 moveto" instruction.
  346.  
  347.         % Shadow box
  348.         gsave
  349.           10 -10 translate
  350.           46 576 moveto % Replaces 46 566 moveto
  351.           540 0 rlineto 0 100 rlineto -540 0 rlineto
  352.           closepath fill
  353.         grestore
  354.  
  355.         % Background box
  356.         gsave
  357.             36 576 moveto
  358.             1 setgray
  359.             540 0 rlineto 0 100 rlineto -540 0 rlineto
  360.             closepath fill
  361.         grestore
  362.  
  363.         % Outline box
  364.         36 576 moveto
  365.         2 setlinewidth
  366.         540 0 rlineto 0 100 rlineto -540 0 rlineto
  367.         closepath stroke
  368.  
  369.         showpage
  370.  
  371.  
  372.    The ability to reuse routines as "procedures" is an important part of
  373.   PostScript and will be explored more fully in tutorial 7.
  374.  
  375.  
  376.    Enlarged Diamond
  377.    Using the homework example from tutorial 1 (if you did not do it you will
  378.   now need to pull your finger out and go and have a bash at it), where we
  379.   created a symmetrical diamond, we can use the 'translate' operator to
  380.   permanently reposition an entire page so that the effect of scaling will
  381.   have no effect on the position of certain objects on the page.
  382.  
  383.    By using this example to scale the diamond using "1.5 1.5 scale", the
  384.   larger diamond moves off the page.
  385.  
  386.         % Original diamond
  387.         486 396 moveto
  388.         -180 180 rlineto -180 -180 rlineto 180 -180 rlineto
  389.         closepath
  390.         stroke
  391.  
  392.         % Scaled diamond
  393.         486 396 moveto
  394.         1.5 1.5 scale
  395.         -180 180 rlineto -180 -180 rlineto 180 -180 rlineto
  396.         closepath
  397.         stroke
  398.  
  399.         showpage
  400.  
  401.  
  402.    Centre of Origin
  403.    Had we planned ahead when we drew the initial diamond we could have drawn
  404.   it in such a way that it could be easily resized.  One way to accomplish
  405.   this would be by repositioning the origin at the centre of the page using
  406.   the following instruction.
  407.  
  408.         306 396 translate
  409.  
  410.  
  411.    Centre Page Grid
  412.    Relocating the origin at the centre of the page allows us to draw diamonds
  413.   of any size, repeating all instructions except for the one that scales each
  414.   diamond.
  415.  
  416.         306 396 translate
  417.  
  418.         gsave
  419.             1 1 scale
  420.             180 0 moveto
  421.             -180 180 rlineto -180 -180 rlineto 180 -180 rlineto
  422.             closepath stroke
  423.         grestore
  424.  
  425.         gsave
  426.             1.5 1.5 scale
  427.             180 0 moveto
  428.             -180 180 rlineto -180 -180 rlineto 180 -180 rlineto
  429.             closepath stroke
  430.         grestore
  431.  
  432.         gsave
  433.             .5 .5 scale
  434.             180 0 moveto
  435.             -180 180 rlineto -180 -180 rlineto 180 -180 rlineto
  436.             closepath stroke
  437.         grestore
  438.  
  439.         showpage
  440.  
  441.    Note how the origin for both the x and y axis is now in the centre of the
  442.   page and how we used negative values to position elements that are to the
  443.   left of or below this new point of origin.  The new origin is reflected in
  444.   the x and y values that accomp any the new grid.
  445.  
  446.  
  447.    Scale and Translate
  448.    You can combine the 'translate' and 'scale' operators to perform specific
  449.   tasks such as reducing the size of a full-page description to one half of
  450.   its normal size.
  451.  
  452.         153 198 translate     % 612/4 and 792/4
  453.         .5 .5 scale           % Half size page
  454.  
  455.         0 0 moveto
  456.         612 0 rlineto 0 792 rlineto -612 0 rlineto
  457.         closepath stroke
  458.  
  459.         showpage
  460.  
  461.    We first translate the point of origin one quarter page right and up from
  462.   the original point of origin, then scale both axis by one half.  Note that
  463.   the rectangle that we drew is for a full 612 by 792 point page, scaled by
  464.   one half.
  465.  
  466.  
  467.    'rotate'
  468.    You can use 'rotate' to reorient the page without affecting the page size
  469.   or origin.  The 'rotate' operator turns the grid counter-clockwise the
  470.   number of degrees specified in the single operand.
  471.  
  472.    As an example, let us show a single line of text perpendicular to text on
  473.   the rest of the page, by rotating the coordinate system 90 degrees
  474.   (temporarily - by using 'gsave' and 'grestore') before stroking the text
  475.   outlines in normal orientation.
  476.  
  477.         /Times-BoldItalic findfont 72 scalefont setfont
  478.  
  479.         gsave
  480.             72 72 moveto
  481.             90 rotate
  482.             (Brisbane) true charpath
  483.         grestore
  484.  
  485.         72 72 moveto
  486.         (Brisbane) true charpath
  487.  
  488.         showpage
  489.  
  490.  
  491.    Negative Rotation
  492.    A negative value for the 'rotate' operand rotates the page in a clockwise
  493.   direction.
  494.  
  495.         /Times-Roman findfont 72 scalefont setfont
  496.  
  497.         306 396 translate
  498.  
  499.         0 0 moveto
  500.         -45 rotate
  501.         (Brisbane) show
  502.  
  503.         showpage
  504.  
  505.  
  506.    Cumulative Rotation
  507.    Like 'scale' and 'rotate' the effects of successive 'rotate' operations
  508.   are cumulative.  In this example we permanently 'translate' the origin to
  509.   the centre of the page, then 'rotate' a fixed number of degrees.
  510.  
  511.         /Times-Roman findfont 50 scalefont setfont
  512.  
  513.         306 396 translate
  514.         -90 rotate
  515.  
  516.         90 rotate
  517.         0 0 moveto
  518.         (0 Brisbane) show
  519.  
  520.         90 rotate
  521.         0 0 moveto
  522.         (90 Brisbane) show
  523.  
  524.         90 rotate
  525.         0 0 moveto
  526.         (180 Brisbane) show
  527.  
  528.         90 rotate
  529.         0 0 moveto
  530.         (270 Brisbane) show
  531.  
  532.         showpage
  533.  
  534.  
  535.    'translate' and 'rotate'
  536.    We can also combine 'translate' and 'rotate' to change the origin and
  537.   orientation of the entire page for all subsequent operations --- a truly
  538.   useful tool for changing a page to "landscape" orientation.
  539.  
  540.    The landscape orientation, which prints perpendicular to the normal
  541.   vertical page orientation, is popular in desktop publishing for creating
  542.   three column brochures and A4 magazines.
  543.  
  544.    In this two step procedure, the first step is to translate the origin from
  545.   the lower left corner to the lower right corner by the following
  546.   instruction.
  547.  
  548.         612 0 translate
  549.  
  550.    Landscape Orientation
  551.    Next a "90 rotate" instruction reorients the page along the vertical axis,
  552.   so that the lower right corner becomes the new lower left corner.
  553.  
  554.         612 0 translate
  555.         90 rotate
  556.  
  557.         /Times-BoldItalic findfont 72 scalefont setfont
  558.  
  559.         72 72 moveto
  560.         (Brisbane Landscape) show
  561.  
  562.         showpage
  563.  
  564.    And voila, we have created a landscape page.
  565.  
  566.  
  567.    'translate', 'rotate' and 'scale'
  568.    Finally we can combine all three transformation operators in one program.
  569.  
  570.         612 0 translate
  571.         90 rotate
  572.  
  573.         /Helvetica-Bold findfont 84 scalefont setfont
  574.         gsave
  575.             396 190 moveto
  576.             1 4 scale
  577.             (DEBATE) show
  578.         grestore
  579.  
  580.         gsave
  581.             .8 setgray
  582.             396 190 moveto
  583.             -1 4 scale
  584.             (DEBATE) show
  585.         grestore
  586.  
  587.         /Times-Bold findfont 72 scalefont setfont
  588.         54 504 moveto
  589.         .8 setgray
  590.         (FORBES v) show
  591.         0 setgray
  592.         (s FULLER) show
  593.  
  594.         /Helvetica-Bold findfont 32 scalefont setfont
  595.         36 72 moveto
  596.         .8 setgray
  597.         1 2 scale
  598.         (CIVIC AUDITORIUM, FRI) show
  599.         0 setgray
  600.         (DAY, MAY 24, 7:30 ) show
  601.         /Helvetica-Bold findfont 30 scalefont setfont
  602.         0 6 moveto
  603.         (p.m.) show
  604.  
  605.         showpage
  606.  
  607.  
  608.    Well that is it for another month, the teacher is too tired from all the
  609.   keyboard input to set an assignment this month, if you do not feel
  610.   comfortable at this point, revision of earlier sessions will definitely
  611.   help alleviate the problem.........See you next month!!!!!!!
  612.  
  613.  
  614.    || 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||| 33 ||
  615.  
  616.